<template>
    <div>
        
        
        <div>
            <el-button size="mini" style="margin:2px 3px" type="primary" @click="outWorkbin" v-loading.fullscreen.lock="fullscreenLoading">缓存箱出库</el-button>
            <el-button size="mini" style="margin:2px 3px" type="primary" @click="saveWorkbin" v-loading.fullscreen.lock="fullscreenLoading">保存</el-button>
            <el-button size="mini" style="margin:2px 3px" type="primary" @click="inWorkbin" v-loading.fullscreen.lock="fullscreenLoading">缓存箱回库</el-button>
            <el-input size="mini" style="width:200px" v-model="workbinCode" placeholder="请输入缓存箱号"></el-input>
        </div>
        <div class="im-iq-topTable">
            <el-table
                ref="receiveTable"
                size="mini"
                :data="resData"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                height="500"
                >
                <el-table-column type="selection" width="40" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="ROW_ID" label="序号" width="80" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="workbinCode" label="缓存箱编号" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="mProductNumber" label="成品型号"  :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="erpOutQty" label="出库数量"  :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="barcode" label="条码" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="itemCode" label="图号" :show-overflow-tooltip="true" > </el-table-column>
                <el-table-column prop="qualityCode" label="质量编号/件号" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="deliveryPlanCode" label="交接单号" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="updateIp" label="当前操作台" :show-overflow-tooltip="true"> </el-table-column>
            </el-table>
                <!-- 分页 -->
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNumber"
                :page-sizes="[10,20,50,100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalCount">
                </el-pagination>
        </div>
    </div>
</template>
<script>
import request from "@/utils/request.js";
export default {
    name:"ImIqTopTable",
    data(){
        return{
             // 当前页数
            pageNumber: 1,
            // 数据总个数
            totalCount: 0,
            // 返回的数据数组
            resData: [],
            // 每页数据个数
            pageSize: 20,
            // 确认列表高亮参数
            stableIndex: 0,
            //勾选的数据
            selectedData:[],
            workbinCode:'',
            fullscreenLoading: false
        }
    },
    methods:{
        // 每页个数改变
        handleSizeChange(val){
            this.pageSize = val
            this.$bus.$emit('imIqPageSize',this.pageSize)
        },
        // 页数改变
        handleCurrentChange(val) {
            this.pageNumber = val;
            this.$bus.$emit('imIqPageNumber',this.pageNumber)
        },
        handleSelectionChange(val){
            this.selectedData = val;
            console.log("勾选数据",val);
            for(let i = 0;i<this.selectedData.length-1;i++){
                if(this.selectedData[i].workbinCode!=this.selectedData[i+1].workbinCode){
                    this.$Alert({
                        title: "失败",
                        message: "请勾选同一物料箱中的物料！",
                        type: "error",
                    });
                    return;
                }
            }
        },
        //erp缓存箱出库
        outWorkbin(){
            if(this.selectedData.length==0){
                this.$Alert({title: "失败",message: '请至少选择一条记录',type: "error"});
                return ;
            }
            for(let i = 0;i<this.selectedData.length-1;i++){
                if(this.selectedData[i].workbinCode!=this.selectedData[i+1].workbinCode){
                    this.$Alert({
                        title: "失败",
                        message: "请勾选同一物料箱中的物料进行出库！",
                        type: "error",
                    });
                    return;
                }
            }
            this.fullscreenLoading = true;
            request.post("/erpOutBill/outWorkbin",{data:this.selectedData[0],datas:this.selectedData}).then((res)=>{
                if(res.data.retCode == 200){
                    this.$Alert({
                        title: "成功",
                        message: res.data.retMsg,
                        type: "success",
                    });
                    this.fullscreenLoading = false;
                }
                else{
                    this.$Alert({
                        title: "失败",
                        message: res.data.retMsg,
                        type: "error",
                    });
                    this.fullscreenLoading = false;
                }
                
            })
            setTimeout(() => {
                    this.fullscreenLoading = false;
                }, 10000);
        },
        //erp物料保存 xiaojian 2023/6/11
        saveWorkbin() {
            if(this.selectedData.length==0){
                this.$Alert({title: "失败",message: '请至少选择一条记录',type: "error"});
                return ;
            }
            for(let i = 0;i<this.selectedData.length-1;i++){
                if(this.selectedData[i].workbinCode!=this.selectedData[i+1].workbinCode){
                    this.$Alert({
                        title: "失败",
                        message: "请勾选同一物料箱中的物料进行保存！",
                        type: "error",
                    });
                    return;
                }
                if(this.selectedData[i].updateIp == null){
                    this.$Alert({
                        title: "失败",
                        message: "保存的物料没有经过叫箱！",
                        type: "error",
                    });
                    return;
                }
            }
            this.fullscreenLoading = true;
            request.post("/erpOutBill/saveWorkbin",{data:this.selectedData}).then((res)=>{
                if(res.data.retCode == 200){
                    this.$Alert({
                        title: "成功",
                        message: res.data.retMsg,
                        type: "success",
                    });
                    this.$bus.$emit("newSearch");
                    this.fullscreenLoading = false;
                }
                else{
                    this.$Alert({
                        title: "失败",
                        message: res.data.retMsg,
                        type: "error",
                    });
                    this.fullscreenLoading = false;
                }
                
            })
            setTimeout(() => {
                    this.fullscreenLoading = false;
                }, 10000);
        },
        inWorkbin() {
            this.fullscreenLoading = true;
            request.post("/erpOutBill/inWorkbin",{data:this.workbinCode}).then((res)=>{
                if(res.data.retCode == 200){
                    this.$Alert({
                        title: "成功",
                        message: res.data.retMsg,
                        type: "success",
                    });
                    this.fullscreenLoading = false;
                }
                else{
                    this.$Alert({
                        title: "失败",
                        message: res.data.retMsg,
                        type: "error",
                    });
                    this.fullscreenLoading = false;
                }
            })
            setTimeout(() => {
                    this.fullscreenLoading = false;
                }, 10000);
        },
    },
    mounted() {
        // 接收TopInput查询到的数据
        this.$bus.$on('ImFqSearchData',(e)=>{
            this.resData = e.retData
            this.totalCount = e.totalCount
        })
  }
}
</script>
<style scoped>
.im-iq-topTable {
    text-align: center;
}
</style>